package com.longrise.common.base.web;

import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.webkit.JavascriptInterface;

import com.longrise.common.R;
import com.longrise.common.base.RxManager;
import com.longrise.common.permission.PermissionUtil;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.DeviceUtils;
import com.longrise.common.utils.PrintLog;
import com.longrise.umeng.share.ShareDialog;
import com.longrise.umeng.share.bean.ShareParams;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by callxkj on 2017/10/17.
 * baseweb的桥梁
 */

public class BaseWebBridge {

    private static final String TAG = "BaseWebBridge";
    public BaseWebActivity mActivity;
    public final RxManager mRxManager;

    public BaseWebBridge(BaseWebActivity activity, RxManager rxManager) {
        this.mActivity = activity;
        this.mRxManager = rxManager;
    }

    /**
     * 设置webview标题
     */
    @JavascriptInterface
    public void setTitle(final String title) {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                observableEmitter.onNext(title);
                observableEmitter.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        if (mActivity != null) {
                            mActivity.setWebTitle(s);
                        }
                    }
                });
        mRxManager.addDisposable(subscribe);
    }

    /**
     * 用于webview的提示
     */
    @JavascriptInterface
    public void showHintMsg(final String msg) {
        if (!TextUtils.isEmpty(msg)) {
            Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
                @Override
                public void subscribe(ObservableEmitter<String> e) throws Exception {
                    e.onNext(msg);
                    e.onComplete();
                }
            }).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<String>() {
                        @Override
                        public void accept(String s) throws Exception {
                            if (mActivity != null) {
                                mActivity.showToast(s);
                            }
                        }
                    });
            mRxManager.addDisposable(subscribe);
        }
    }

    /**
     * 积分提示框
     */
    // infostate: 1:成功  .. 0:完成失败(失败后由网页调用showHintMsg方法去显示提示),
    @JavascriptInterface
    public void aboutIntegral(String infostate, String acquiredscore, String remindwords) {
        if ("1".equals(infostate)) {
//                MyIntegralDialogUtil.showIntegralDailog(NewsDetailActivity.this,acquiredscore,remindwords);
        }
    }

    /**
     * 获取设备的版本号 用于谷歌分析
     */
    @JavascriptInterface
    public void getDeviceInfor() {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("");
                e.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        mActivity.loadUrl("javascript:setDeviceInfoForGA('" + DeviceUtils.getVersionName() + "')");
                    }
                });
        mRxManager.addDisposable(subscribe);
    }

    /**
     * web调用这个方法 通知我们关闭webview
     */
    @JavascriptInterface
    public void finishWeb() {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("11");
                e.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        mActivity.close();
                    }
                });
        mRxManager.addDisposable(subscribe);
    }


    /**
     * 打电话的方法
     */
    @JavascriptInterface
    public void call(final String phone) {
        if (TextUtils.isEmpty(phone)) {
            return;
        }
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext(phone);
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        toGetPhonePermission(s);
                    }
                });
        mRxManager.addDisposable(subscribe);
    }

    /**
     * 分享
     * @param url
     * @param title
     * @param content
     * @param iconUrl
     */
    @JavascriptInterface
    public void shareParams(final String title, final String content, final String url,final String iconUrl) {
        if (!TextUtils.isEmpty(url) && !TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
            Disposable subscribe = Observable.create(new ObservableOnSubscribe<ShareParams>() {
                @Override
                public void subscribe(ObservableEmitter<ShareParams> e) throws Exception {
                    ShareParams shareParams = new ShareParams();
                    shareParams.mUrl = url;
                    shareParams.mContent = content;
                    shareParams.mHeadUrl = iconUrl;

                    shareParams.mTitle = title;
                    e.onNext(shareParams);
                    e.onComplete();

                }
            }).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<ShareParams>() {
                        @Override
                        public void accept(ShareParams shareParams) throws Exception {
                            ShareDialog shareDialog = new ShareDialog(mActivity);
                            shareDialog.setShareParame(shareParams);
                            shareDialog.setCallback(new ShareDialog.ShareCallback() {
                                @Override
                                public void shareSuccess() {
                                    PrintLog.e("mainactivity","111111111111");
                                }

                                @Override
                                public void shareFailed() {
                                    PrintLog.e("mainactivity","222222222222");
                                }
                            });
                            shareDialog.show();
                        }

                    });
            mRxManager.addDisposable(subscribe);
        }
    }



    /**
     * 获取电话的权限 并且拨打电话
     * @param phone 电话号码
     */
    private void toGetPhonePermission(final String phone){
        PermissionUtil.getInstance().requestPermission(mActivity ,PermissionUtil.Permission.PHONE,
                AppUtil.getString(R.string.call_permission), AppUtil.getString(R.string.call_permission_hint),
                new PermissionUtil.PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        if (!TextUtils.isEmpty(phone)) {
                            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            mActivity.startActivity(intent);
                        }
                    }
                    @Override
                    public void onDenied() {

                    }
                });

    }





}
